﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DJJuninho.Web.News.Item
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                if (!IsPostBack)
                {

                    Populate();

                    int id;
                    if (int.TryParse(Request["id"], out id))
                    {

                        LoadData(id);

                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void Populate()
        {
            try
            {

                lblDateTip.Text = DateTime.Now.ToString();
                txtCreatedDate.Text = lblDateTip.Text;

                ddlCategorySection.DataSource = Business.CategorySection.SelectOrderingByName();
                ddlCategorySection.DataBind();

                ddlAlbum.DataSource = Business.Album.SelectOrderingByName();
                ddlAlbum.DataBind();
            
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void LoadData(int id)
        {
            try
            {

                var obj = int.TryParse(Request["id"], out id) ? new Business.News(id) : new Business.News();

                txtName.Text = obj.Name;
                ddlCategorySection.SelectedValue = obj.CategorySectionId.ToString();
                txtHtmlBody.Text = obj.HtmlBody;
                imgPhoto.ImageUrl = ResolveUrl("~/uploads/" + obj.Photo);
                imgMiniPhoto.ImageUrl = ResolveUrl("~/uploads/" + obj.MiniPhoto);
                txtCreatedDate.Text = obj.CreatedDate.ToString();
                ddlAlbum.SelectedValue = obj.AlbumId.ToString();
                                
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {

                Validate();

                if (IsValid)
                {

                    int id;
                    var obj = int.TryParse(Request["id"], out id) ? new Business.News(id) : new Business.News();

                    obj.Name = txtName.Text;

                    obj.Description = txtDescription.Text;

                    int categorySectionId;

                    if (int.TryParse(ddlCategorySection.SelectedValue, out categorySectionId))
                    {

                        obj.CategorySectionId = categorySectionId;
                    
                    }

                    obj.HtmlBody = txtHtmlBody.Text;

                    if (txtPhoto.HasFile)
                    {

                        obj.Photo = DateTime.Now.Ticks + System.IO.Path.GetExtension(txtPhoto.PostedFile.FileName);

                        txtPhoto.PostedFile.SaveAs(Server.MapPath("~/uploads/") + obj.Photo);

                    }

                    if (txtMiniPhoto.HasFile)
                    {

                        obj.MiniPhoto = DateTime.Now.Ticks + System.IO.Path.GetExtension(txtMiniPhoto.PostedFile.FileName);

                        txtMiniPhoto.PostedFile.SaveAs(Server.MapPath("~/uploads/") + obj.MiniPhoto);

                    }

                    DateTime createdDate;

                    if (DateTime.TryParse(txtCreatedDate.Text, out createdDate)) {

                        obj.CreatedDate = createdDate;
                    
                    }

                    int albumId;

                    if (int.TryParse(ddlAlbum.SelectedValue, out albumId))
                    {

                        obj.AlbumId = albumId;

                    }

                    obj.Save();

                    Response.Redirect("../");

                }

            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
